<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2019/3/20
 * Time: 22:06
 */

namespace App\Http\Controllers\Api\V1;

use App\Http\Controllers\Api\ApiController;
use App\Models\Allowance;
use App\Models\District;
use App\Models\Loss;
use App\Models\Order;
use App\Models\OwnerRoutes;
use App\Models\Shipping;
use App\Models\ShippingPay;
use Illuminate\Http\Request;
use Validator;

class OwnerRoutesController extends ApiController
{
    /**
     * 常用路线列表
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function routeList(Request $request)
    {
        try {
            $validator = Validator::make($request->all(), [
                'owner_uuid' => 'required|size:36',
                'page'       => 'required|integer',
                'limit'      => 'required|integer'
            ]);
            if ($validator->fails()) {
                throw new \Exception($validator->errors()->first());
            }

            $res = OwnerRoutes::select(['*'])
                ->where('owner_uuid', $request->get('owner_uuid'))
                ->paginate($request->get('limit', self::DEFAULT_PAGE_LIMIT))
                ->toArray();
            if (empty($res)) {
                return $this->success();
            }
            $oDistrict = new District();
            foreach ($res['data'] as $key => $value) {
                $container = [];
                array_push($container, $value['src_province_code']);
                array_push($container, $value['src_city_code']);
                array_push($container, $value['src_district_code']);

                array_push($container, $value['dst_province_code']);
                array_push($container, $value['dst_city_code']);
                array_push($container, $value['dst_district_code']);

                $result = $oDistrict->getNameByAdcode($container);

                $res['data'][ $key ]['route_id']          = $value['id'];
                $res['data'][ $key ]['src_province_name'] = $result[ $value['src_province_code'] ];
                $res['data'][ $key ]['src_city_name']     = $result[ $value['src_city_code'] ];
                $res['data'][ $key ]['src_district_name'] = $result[ $value['src_district_code'] ];
                $res['data'][ $key ]['src_address']       = $value['src_address'];
                $res['data'][ $key ]['src_location']      = $value['src_location'];

                $res['data'][ $key ]['dst_province_name'] = $result[ $value['dst_province_code'] ];
                $res['data'][ $key ]['dst_city_name']     = $result[ $value['dst_city_code'] ];
                $res['data'][ $key ]['dst_district_name'] = $result[ $value['dst_district_code'] ];
                $res['data'][ $key ]['dst_address']       = $value['dst_address'];
                $res['data'][ $key ]['dst_location']      = $value['dst_location'];

                $res['data'][ $key ]['driving_distance'] = $value['driving_distance'];
                $res['data'][ $key ]['line_distance']    = $value['line_distance'];
            }

            return $this->table($res['data'], $res['total']);

        } catch (\Exception $e) {
            return $this->error($e->getMessage());
        }
    }

    /**
     * 添加路线
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function addRoute(Request $request)
    {
        try {
            $validator = Validator::make($request->all(), [
                'owner_uuid'        => 'required|size:36',
                'src_province_code' => 'required',
                'src_city_code'     => 'required',
                'src_district_code' => 'required',
                'src_address'       => 'required',
                'dst_province_code' => 'required',
                'dst_city_code'     => 'required',
                'dst_district_code' => 'required',
                'dst_address'       => 'required',
            ]);
            if ($validator->fails()) {
                throw new \Exception($validator->errors()->first());
            }

            $data['owner_uuid']        = $request->get('owner_uuid');
            $data['src_province_code'] = $request->get('src_province_code');
            $data['src_city_code']     = $request->get('src_city_code');
            $data['src_district_code'] = $request->get('src_district_code');
            $data['src_address']       = $request->get('src_address');
            $data['dst_province_code'] = $request->get('dst_province_code');
            $data['dst_city_code']     = $request->get('dst_city_code');
            $data['dst_district_code'] = $request->get('dst_district_code');
            $data['dst_address']       = $request->get('dst_address');

            $data['src_location'] = Amap::geo($request->get('src_address'), $request->get('src_city_code'))->location;
            $data['dst_location'] = Amap::geo($request->get('dst_city_code'), $request->get('dst_city_code'))->location;
            if (!empty($data['src_location']) && !empty($data['dst_location'])) {
                // 驾驶距离
                $data['driving_distance'] = Amap::distance($data['src_location'], $data['dst_location'], 1)[0]->distance;

                // 直线距离
                $data['line_distance'] = Amap::distance($data['src_location'], $data['dst_location'], 0)[0]->distance;
            }

            OwnerRoutes::insert($data);

            return $this->success('success');
        } catch (\Exception $e) {
            return $this->error($e->getMessage());
        }
    }

}